package com.fingersoft.feature.personal;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.fingersoft.app.bean.AppUser;
import com.fingersoft.business.BusinessContext;
import com.fingersoft.business.appstore.IAppStoreProvider;
import com.fingersoft.business.appupdate.IAppUpdateProvider;
import com.fingersoft.common.ICallback;
import com.fingersoft.common.preference.AppPreferenceHelper;
import com.fingersoft.common.preference.UserAppPreferenceHelper;
import com.fingersoft.common.preference.provider.UserIdProvider;
import com.fingersoft.feature.appupdate.CheckCallBack;
import com.fingersoft.feature.faceId.FaceIDExport;
import com.fingersoft.feature.filemanager.ui.FileManagerActivityKt;
import com.fingersoft.feature.filemanager.ui.YunPanActivity;
import com.fingersoft.feature.login.APIUtils2;
import com.fingersoft.feature.login.LoginActivity;
import com.fingersoft.feature.login.TelAlertDialog;
import com.fingersoft.feature.newlock.LockContext;
import com.fingersoft.feature.newlock.LockManager;
import com.fingersoft.feature.personal.api.BindingDeviceBean;
import com.fingersoft.feature.personal.api.DeviceDetailBean;
import com.fingersoft.feature.personal.api.DeviceDetailResponse2;
import com.fingersoft.feature.personal.api.DeviceListResponse2;
import com.fingersoft.feature.personal.api.FeedBackFileParam;
import com.fingersoft.feature.personal.api.FeedBackFileResponse2;
import com.fingersoft.feature.personal.api.FeedBackResponse2;
import com.fingersoft.feature.personal.api.LoginLogBean;
import com.fingersoft.feature.personal.api.LoginLogResponse2;
import com.fingersoft.feature.personal.api.PublicServiceContactParam;
import com.fingersoft.feature.personal.api.PublicServiceContactResponse;
import com.fingersoft.feature.personal.api.SetPasswordResponse2;
import com.fingersoft.feature.personal.api.UserDetailBean;
import com.fingersoft.feature.personal.api.UserDetailResponse2;
import com.fingersoft.feature.personal.api.VersionLogBean;
import com.fingersoft.feature.personal.api.VersionLogResponse2;
import com.fingersoft.feature.personal.messagecenter.MessageCenterEntity;
import com.fingersoft.feature.personal.messagecenter.MessageCenterUpdateParam;
import com.fingersoft.feature.personal.messagecenter.db.PersonalRealm;
import com.fingersoft.feature.userinfo.model.User;
import com.fingersoft.im.api.UserAgent;
import com.fingersoft.im.api.base.BaseModelCallback2;
import com.fingersoft.im.api.base.BaseResponse2;
import com.fingersoft.im.api.model.UserInfo;
import com.fingersoft.im.base.MyActivityManager;
import com.fingersoft.im.event.EventManager;
import com.fingersoft.im.utils.AllCollectParam;
import com.fingersoft.im.utils.AppConfigUtils;
import com.fingersoft.im.utils.AppUtils;
import com.fingersoft.im.utils.ApplicationUtils;
import com.fingersoft.im.utils.FileUtils;
import com.fingersoft.im.utils.JSONUtils;
import com.fingersoft.im.utils.UserInfoManager;
import com.fingersoft.im.view.LoadDialog;
import com.fingersoft.realm.AppRealmUtils;
import com.fingersoft.theme.ThemeM;
import com.fingersoft.util.AppEventType;
import com.fingersoft.util.AppEventUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.request.BaseRequest;
import com.shougang.call.util.PinyinUtils;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

import static com.fingersoft.feature.theme.activity.ThemeActivityKt.startThemeActivity;

/**
 *
 * @author wc
 * @date 2017/9/28
 */

public class SealPersonalContext extends SealPersonalContext2   {


    private final Application application;

    public SealPersonalContext(Application application) {

        this.application = application;
        BusinessContext.Companion.getUserBehaviorListeners().add(this);
    }

    @Override
    public Boolean isFaceRecognitionSwitchOn() {
        return FaceIDExport.Companion.isSwitchOn();
    }

    @Override
    public Boolean isFaceRecognitionEnable() {
        return FaceIDExport.Companion.isModuleEnable();
        //return AppUtils.useFaceID();
    }

    @Override
    public void setFaceRecognitionSwitchOn(boolean ison) {
        FaceIDExport.Companion.setFaceIdSwitchStatus(ison);
    }

    @Override
    public void gotoFaceRecognitionSetting(Context context) {
        FaceIDExport.Companion.startSetting(context);
    }

    @Override
    public Boolean isCollection() {
        return AppUtils.isCollection();
    }

    @Override
    public void doLoginOut(Context context) {
        AppEventUtil.writeLandingState(AppEventType.AppLogout);
        getUserAppPreferenceHelper().putBoolean("isJustLogout", true);
        new AppPreferenceHelper().putBoolean("isNotKickOut", true);
        onModuleStart(context);
        //调用退出登录接口,接口只能被调用一次
        new APIUtils2().logout(new ICallback<String>() {
            @Override
            public void onSuccess(String data) {
                doWhenLogoutComplete(context);
            }

            @Override
            public void onError() {
                doWhenLogoutComplete(context);
            }
        });
    }

    private void doWhenLogoutComplete(Context context) {
        LockManager.Companion.getInstance().doLoginOut();
        MyActivityManager.getInstance().finishAllActivity();//关闭所有页面
        UserInfo userInfo = UserInfoManager.INSTANCE.getCurrentUserInfo();
        LockManager.Companion.setLoginUserAvatar(userInfo.getIcon());
        LockManager.Companion.setLoginUserName(userInfo.getName());
        AppUtils.logout(context);
    }

    @Override
    public void jumpTheme(Context context) {
        startThemeActivity(context);
    }


    @Override
    public void gotoMyFiles(Context context) {
        FileManagerActivityKt.startFileManagerActivity(context);
    }
    @Override
    public void gotoMyYunPan(Context context) {
        YunPanActivity.startYunPanActivity(context,"0");
    }

    @Override
    public Boolean useWriteSignature() {
        return AppConfigUtils.getAppConfigInfo(this.application).getUseWriteSignature();
    }

    @Override
    public Boolean useScan() {
        return AppConfigUtils.getAppConfigInfo(this.application).getUseScan();
    }

    @Override
    public Boolean useFileManager() {
        return AppUtils.useFileManager();
    }

    @Override
    public Boolean isUseTheme() {
        return AppUtils.useTheme();
    }

    @Override
    public Boolean isUseWorkCircle() {
        return AppUtils.useWorkcircle();
    }


    @Override
    public int getThemeColor() {
        return ThemeM.getThemeColor();
    }

    @Override
    public Boolean isUseAppStore() {
        return AppUtils.useAppStore();
    }

    @Override
    public Object getConfig(Context context) {
        return AppConfigUtils.getAppConfigInfo(context);
    }

    @Override
    public void getLoginLog(Activity context, String path, String params, ICallback<List<LoginLogBean>> callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<LoginLogResponse2>(LoginLogResponse2.class) {
                    @Override
                    public void onSuccess(LoginLogResponse2 loginLogResponse2, Call call, Response response) {
                        super.onSuccess(loginLogResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(loginLogResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(loginLogResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onAfter(LoginLogResponse2 loginLogResponse2, Exception e) {
                        super.onAfter(loginLogResponse2, e);
                    }
                });
    }

    @Override
    public void setPassword(@NonNull Context context, @Nullable String path, @Nullable String params, @NonNull ICallback callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(context, AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<SetPasswordResponse2>(SetPasswordResponse2.class) {
                    @Override
                    public void onSuccess(SetPasswordResponse2 setPasswordResponse2, Call call, Response response) {
                        super.onSuccess(setPasswordResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(setPasswordResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(setPasswordResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onAfter(SetPasswordResponse2 setPasswordResponse2, Exception e) {
                        super.onAfter(setPasswordResponse2, e);
                    }
                });
    }

    @Override
    public void getFace_user_permission(Context context, String path, ICallback callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
            .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(context, AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
            //.upJson(params)
            .execute(new BaseModelCallback2<SetPasswordResponse2>(SetPasswordResponse2.class) {
                @Override
                public void onSuccess(SetPasswordResponse2 setPasswordResponse2, Call call, Response response) {
                    super.onSuccess(setPasswordResponse2, call, response);
                    callback.onSuccess(setPasswordResponse2.getCode());
                }

                @Override
                public void onError(Call call, Response response, Exception e) {
                    super.onError(call, response, e);
                    callback.onError();
                }

                @Override
                public void onBefore(BaseRequest request) {
                    super.onBefore(request);
                }

                @Override
                public void onAfter(SetPasswordResponse2 setPasswordResponse2, Exception e) {
                    super.onAfter(setPasswordResponse2, e);
                }
            });
    }

    @Override
    public void getEnterConfig(Context context, String path, String params, ICallback callback) {
        OkGo.post("https://empcloud.ishangban.com/emp-cloud/i/enterprise/queryByAlia")
               //.headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(context, AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<SetPasswordResponse2>(SetPasswordResponse2.class) {
                    @Override
                    public void onSuccess(SetPasswordResponse2 setPasswordResponse2, Call call, Response response) {
                        super.onSuccess(setPasswordResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(setPasswordResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(setPasswordResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onAfter(SetPasswordResponse2 setPasswordResponse2, Exception e) {
                        super.onAfter(setPasswordResponse2, e);
                    }
                });
    }

    @Override
    public void getPublicServiceContact(Context context, String path, String params, ICallback<List<PublicServiceContactParam>> callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<PublicServiceContactResponse>(PublicServiceContactResponse.class) {
                    @Override
                    public void onSuccess(PublicServiceContactResponse getCollectResponse, Call call, Response response) {
                        super.onSuccess(getCollectResponse, call, response);
                        if (AppUtils.showApiSucceedErrorToast(getCollectResponse)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(getCollectResponse.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                    }

                    @Override
                    public void onAfter(PublicServiceContactResponse getCollectResponse, Exception e) {
                        super.onAfter(getCollectResponse, e);
                    }
                });
    }

    @Override
    public void getVersionList(Activity context, String path, ICallback<List<VersionLogBean>> callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .execute(new BaseModelCallback2<VersionLogResponse2>(VersionLogResponse2.class) {
                    @Override
                    public void onSuccess(VersionLogResponse2 versionLogResponse2, Call call, Response response) {
                        super.onSuccess(versionLogResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(versionLogResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(versionLogResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(VersionLogResponse2 versionLogResponse2, Exception e) {
                        super.onAfter(versionLogResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void updateFeedBack(Activity context, String path, String params, ICallback callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(context, AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<FeedBackResponse2>(FeedBackResponse2.class) {
                    @Override
                    public void onSuccess(FeedBackResponse2 feedBackResponse2, Call call, Response response) {
                        super.onSuccess(feedBackResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(feedBackResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(feedBackResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(FeedBackResponse2 feedBackResponse2, Exception e) {
                        super.onAfter(feedBackResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void saveIcon(String path) {
        AppUtils.updateUserIcon(path);
    }

    @Override
    public void clearCache() {
        BusinessContext.Companion.getCacheProvider().clearCache(this.application);
    }

    @Override
    public void checkforUpdate(Activity activity) {
        IAppUpdateProvider appUpdate = BusinessContext.Companion.getAppUpdate();
        if (appUpdate != null) {
            appUpdate.checkUpdate(activity, new CheckCallBack() {
                @Override
                public void onCancel() {

                }

                @Override
                public void onDownLoad(boolean isForcely) {

                }

                @Override
                public void onNowVersion() {

                }

                @Override
                public void onError() {

                }

                @Override
                public void onNewVersion() {

                }
            });
        }
    }

    @Override
    public String getCurrentVersion(Context context) {
        return ApplicationUtils.getVersionName(context);
    }

    @Override
    public String getCodeVersion(Context context) {
        return AppUtils.getCodeVersion();
    }

    @Override
    public String getCacheSize() {
        long size = BusinessContext.Companion.getCacheProvider().getCacheSize(this.application);
        return FileUtils.readableFileSize(size);
    }

    public UserAppPreferenceHelper getUserAppPreferenceHelper() {
        return new UserAppPreferenceHelper(new UserIdProvider() {
            @NotNull
            @Override
            public String getUserId() {
                return AppUtils.getUser().getUserId();
            }
        });
    }

    public void logout() {
        EventBus.getDefault().post(new EventManager.OnRNLogout());
    }

    @Override
    public void login(Context  context) {
        LoginActivity.start(context);
    }

    @Override
    public void login(Context context, boolean doPreConfig) {
        LoginActivity.startWithPreConfig(context, doPreConfig);
    }

    @Override
    public void showAlert(Context context, String title, String message) {
        TelAlertDialog  tl=new TelAlertDialog(context,title,message);
        tl.show();
    }

    @Override
    public void updateFeedBackFile(Activity context, String path, FeedBackFileParam param, ICallback callback) {

        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .params("dirType", param.dirType)
                .addFileParams("file", param.files)
                .execute(new BaseModelCallback2<FeedBackFileResponse2>(FeedBackFileResponse2.class) {
                    @Override
                    public void onSuccess(FeedBackFileResponse2 feedBackFileResponse2, Call call, Response response) {
                        super.onSuccess(feedBackFileResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(feedBackFileResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(feedBackFileResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(FeedBackFileResponse2 feedBackFileResponse2, Exception e) {
                        super.onAfter(feedBackFileResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void getFingerPrintEable() {
        if (AppUtils.useLock()) {
            LockContext.instance.doFingerPrintCheck();
        }
    }

    @Override
    public void getDeviceList(Activity context, String path, ICallback<List<BindingDeviceBean>> callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .execute(new BaseModelCallback2<DeviceListResponse2>(DeviceListResponse2.class) {
                    @Override
                    public void onSuccess(DeviceListResponse2 deviceListResponse2, Call call, Response response) {
                        super.onSuccess(deviceListResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(deviceListResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(deviceListResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(DeviceListResponse2 deviceListResponse2, Exception e) {
                        super.onAfter(deviceListResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void UploadAppEventLog(Activity context, String path, File param, ICallback callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                //.params("dirType", param.dirType)
                .params("file", param)
                .execute(new BaseModelCallback2<SetPasswordResponse2>(SetPasswordResponse2.class) {
                    @Override
                    public void onSuccess(SetPasswordResponse2 feedBackFileResponse2, Call call, Response response) {
                        super.onSuccess(feedBackFileResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(feedBackFileResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(feedBackFileResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        //LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(SetPasswordResponse2 feedBackFileResponse2, Exception e) {
                        super.onAfter(feedBackFileResponse2, e);
                        //LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public String getPinYin(String s) {
        return PinyinUtils.getPinYin(s);
    }

    @Override
    public String getFirstSpell(String s) {
        return PinyinUtils.getFirstSpell(s);
    }

    @Override
    public Boolean doCheckFingerPrint(Context context) {
        return LockManager.Companion.getInstance().checkFingerprint();
    }

    @Override
    public void checkForFingerPrint() {
        LockContext.instance.doFingerprintAuthenticate();
    }

    @Override
    public void onModuleStart(Context context) {
        User user = AppUtils.getUser();
        LockManager.Companion.getInstance().init(context, user.getPassword(), user.getEmpLivingPhoto(), user.getRealName(),user.getGender(), user.getJobNnumber());
    }

    @Override
    public void onModuleDestory(Context context) {
        LockContext.instance.onModuleDestory(context);
    }

    @Override
    public void startAppStore(@NonNull Context context) {
        IAppStoreProvider appstore = BusinessContext.Companion.getAppstore();
        if (appstore != null) {
            appstore.startAppStore(context);
        }
    }

    @Override
    public void setDeviceEnable(Activity context, String path, String params, ICallback callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<BaseResponse2>(BaseResponse2.class) {
                    @Override
                    public void onSuccess(BaseResponse2 baseResponse2, Call call, Response response) {
                        super.onSuccess(baseResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(baseResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(baseResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(BaseResponse2 baseResponse2, Exception e) {
                        super.onAfter(baseResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void setDeviceLose(Activity context, String path, String params, ICallback callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<BaseResponse2>(BaseResponse2.class) {
                    @Override
                    public void onSuccess(BaseResponse2 baseResponse2, Call call, Response response) {
                        super.onSuccess(baseResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(baseResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(baseResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(BaseResponse2 baseResponse2, Exception e) {
                        super.onAfter(baseResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void setDeviceUnbind(Activity context, String path, String params, ICallback callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<BaseResponse2>(BaseResponse2.class) {
                    @Override
                    public void onSuccess(BaseResponse2 baseResponse2, Call call, Response response) {
                        super.onSuccess(baseResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(baseResponse2) | AppUtils.checkDeviceEnableForApiBody(baseResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(baseResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(BaseResponse2 baseResponse2, Exception e) {
                        super.onAfter(baseResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void getDeviceDetail(Activity context, String path, String params, ICallback<DeviceDetailBean> callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson(params)
                .execute(new BaseModelCallback2<DeviceDetailResponse2>(DeviceDetailResponse2.class) {
                    @Override
                    public void onSuccess(DeviceDetailResponse2 deviceDetailResponse2, Call call, Response response) {
                        super.onSuccess(deviceDetailResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(deviceDetailResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(deviceDetailResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(DeviceDetailResponse2 deviceDetailResponse2, Exception e) {
                        super.onAfter(deviceDetailResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }

    @Override
    public void getUserDetail(Activity context, String path, ICallback<UserDetailBean> callback) {
        OkGo.post(AppUtils.getApiUrl(path, AppUtils.J_ECODE))
                .headers("Emp-Info", JSONUtils.toJsonString(new UserAgent(AppUtils.getTokenInfo().getUserToken(), AppUtils.getTokenInfo().getDid())))
                .upJson("")
                .execute(new BaseModelCallback2<UserDetailResponse2>(UserDetailResponse2.class) {
                    @Override
                    public void onSuccess(UserDetailResponse2 userDetailResponse2, Call call, Response response) {
                        super.onSuccess(userDetailResponse2, call, response);
                        if (AppUtils.showApiSucceedErrorToast(userDetailResponse2)) {
                            callback.onError();
                            return;
                        }
                        callback.onSuccess(userDetailResponse2.getData());
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        callback.onError();
                    }

                    @Override
                    public void onBefore(BaseRequest request) {
                        super.onBefore(request);
                        LoadDialog.show(context);
                    }

                    @Override
                    public void onAfter(UserDetailResponse2 userDetailResponse2, Exception e) {
                        super.onAfter(userDetailResponse2, e);
                        LoadDialog.dismiss(context);
                    }
                });
    }


    @Override
    public Application getApplication() {
        return application;
    }

    @Override
    public void removeWork() {
        //读取时间戳
        AppRealmUtils.Companion.getRealm().beginTransaction();
        //查询用户
        AppUser workUser = AppRealmUtils.Companion.getRealm().where(AppUser.class).equalTo("name", AppUtils.getUser().getUserName()).findFirst();
        //没有用户则新建用户
        if (workUser == null) {
            workUser = AppRealmUtils.Companion.getRealm().createObject(AppUser.class, AppUtils.getUser().getUserName());
        }
        workUser.setWorkHomeAppListTimeStamp("0");
        workUser.setIsWorkHomeAppRefrash(true);
        AppRealmUtils.Companion.getRealm().commitTransaction();

    }

    @Override
    public void saveCollect(List<AllCollectParam> list) {
        if(BusinessContext.Companion.getCollect() !=null){
            BusinessContext.Companion.getCollect().saveCollect(list);
        }
    }

    @Override
    public void disconnectVPN() {
        if(BusinessContext.Companion.getMVPNProvider()!=null){
            BusinessContext.Companion.getMVPNProvider().cleanVPNAccount();
        }
    }

    @Override
    public Boolean showDisconnectVPNButton() {
        return AppUtils.showDisconnectVPNButton();
    }

    @Override
    public void saveMessageCenterData(ArrayList<MessageCenterEntity> messageCenterParams) {
        PersonalRealm.Companion.getInstance().init(application);
        if (null == messageCenterParams || messageCenterParams.size() <= 0) {
            return;
        }
        for (int i = 0; i < messageCenterParams.size(); i++) {
            final int index = i;
            PersonalRealm.Companion.getInstance().getRealm().executeTransaction((realm) -> {
                    realm.insertOrUpdate(messageCenterParams.get(index).convert2MessageCenterParam());
                    MessageCenterUpdateParam updateParam = new MessageCenterUpdateParam();
                    updateParam.setTimestamp(System.currentTimeMillis());
                    realm.insertOrUpdate(updateParam);
                }
            );
        }
    }

}
